Skip to content

feat(libkernel): add process and memory lock exports - #3650

Closed
MarcZX10 wants to merge 3 commits into
boykopovar:mainfrom
MarcZX10:codex/libkernel-xpsemu-process-3628
Closed

MarcZX10 wants to merge 3 commits into
boykopovar:mainfrom
MarcZX10:codex/libkernel-xpsemu-process-3628

Conversation

@MarcZX10

Copy link
Copy Markdown
Contributor

What

Carries the three commits from David's ready branch for issue #3628, preserving his authorship. Exports pthread_atfork, mlockall, and getdtablesize so XPSemu and VLC can resolve their imports. Their next startup stops are predicted from registry order and have not been observed in a title run.

  • pthread_atfork records prepare, parent, and child handlers in registration order and returns ENOMEM on allocation failure. Handlers are not run because fork throws and vfork is absent; the limitation is documented in TechnicalDebt.
  • mlockall returns EINVAL for invalid flags and throws for valid requests because locking the whole host process or all future guest and host mappings is not implemented.
  • getdtablesize throws because the console's RLIMIT_NOFILE soft limit and kern.maxfilesperproc are unknown. Returning a guessed limit would cause callers' closefrom loops to close a guessed descriptor range.

Semantics are based on FreeBSD 11.4 (8c639945); the issue body records the source references and title call sites.

Tested

  • Linux, GCC 12.2, Release: built the three affected test targets (guest_pthread_atfork_tests, guest_mlock_tests, guest_getrlimit_tests); CTest passed all three.
  • python3 tools/check_conventions.py --base upstream/main and git diff --check upstream/main...HEAD passed.
  • The issue author reports a Windows 11 Pro 26220, WinLibs GCC 15.2.0 Release full build and the new cases passing in the relevant existing tests; see #3628. I did not independently run Windows or a title.

Checklist

  • Based on current main; no other open PR implements these functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

David and others added 3 commits October 11, 2026 14:41
pthread_atfork (U9t5kJAWPnA, aerolib) appends the prepare, parent and
child handlers to a process-wide list under a lock and returns 0, or
ENOMEM when the entry cannot be allocated, as FreeBSD 11.4's
_pthread_atfork does (lib/libthr/thread/thr_fork.c:78-100, freebsd-src
8c639945a44588f6cfdddfb057bb5aad88735da0). Null handlers are accepted;
FreeBSD's fork skips them. The handlers never run: fork
(libkernel_sys) throws and vfork is absent, so no child process is
created. The list is kept, in registration order, for a later fork
(PthreadAtforkHandlers_nid_no_patch); recorded in TechnicalDebt.

XPSemu (PPSA97358) calls it from a constructor, before main
(ZiZc3/XPSemu 8eec07b4, util/rcu.c:488-495):

    static void __attribute__((__constructor__)) rcu_init(void)
    {
        smp_mb_global_init();
    #ifdef CONFIG_POSIX
        pthread_atfork(rcu_init_lock, rcu_init_unlock, rcu_init_child);
    #endif
        rcu_init_complete();
    }

so a throwing export would stop the title before main. Predicted stop
removed (registry index 255, not run):
FAIL: unresolved ELF import U9t5kJAWPnA from libkernel.prx

guest_pthread_atfork checks registration order, null handlers,
concurrent registration from four threads and that no handler runs.


Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
mlockall (x7g7Ebeo8-U, aerolib) follows FreeBSD 11.4's sys_mlockall
(sys/vm/vm_mmap.c:1067-1131, freebsd-src
8c639945a44588f6cfdddfb057bb5aad88735da0) for the flag check: 0 or
bits outside MCL_CURRENT (1) and MCL_FUTURE (2) fail with EINVAL. The
title process is taken to pass priv_check(PRIV_VM_MLOCK), as mlock
already does.

Valid flags throw. MCL_FUTURE would need every later guest and host
mapping to be locked; nothing in the mapping paths does that, and
passing it to the host would make later host allocations fail above
the host RLIMIT_MEMLOCK. MCL_CURRENT would have to lock the whole
process, which on AnyPS5 also holds host stacks, heaps, modules and
driver mappings (sceKernelVirtualQuery reports them as guest memory),
with ENOMEM above a guest RLIMIT_MEMLOCK that is not known (getrlimit
throws for it). mlock pins host pages (VirtualLock, mlock) range by
range and keeps no record, so there is no state to extend. Recorded
in TechnicalDebt.

XPSemu (PPSA97358) calls it only with -overcommit mem-lock=on
(ZiZc3/XPSemu 8eec07b4, os-posix.c:330-355, reached from
system/vl.c:831-835):

    int flags = MCL_CURRENT | MCL_FUTURE;
    ...
    ret = mlockall(flags);
    if (ret < 0) {
        error_report("mlockall: %s", strerror(errno));
    }

and exits on failure, so that option ends in the throw; without it
the title is not affected. Predicted stop removed (registry index 201,
not run):
FAIL: unresolved ELF import x7g7Ebeo8-U from libkernel.prx

guest_mlock checks EINVAL for 0, 4, 5 and -1 and the throw for 1, 2
and 3.


Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
getdtablesize (sZuwaDPATKs, aerolib) is a throwing export. FreeBSD
11.4 makes it system call 89, sys_getdtablesize
(sys/kern/kern_descrip.c:338-355, freebsd-src
8c639945a44588f6cfdddfb057bb5aad88735da0):
min(lim_cur(RLIMIT_NOFILE), maxfilesperproc); FreeBSD 11.4 has no
lib/libc/gen/getdtablesize.c. Neither the console's RLIMIT_NOFILE soft
limit nor kern.maxfilesperproc is published: shadPS4 443f0224 has no
getdtablesize or RLIMIT_NOFILE (its posix_sysconf table answers
_SC_OPEN_MAX with -1, without a stated source), KytyPS5 4733b7e and
OpenOrbis 0a1aaf9d have neither, fpPS4 04cefd43 returns High(Integer)
as a placeholder, SharpProspero 5dfdb4b0 only binds it. getrlimit
keeps throwing for RLIMIT_NOFILE for the same reason. Recorded in
TechnicalDebt.

VLC (PPSA85300) and XPSemu (PPSA97358) import it and call it only from
their own closefrom (ZiZc3/VLC f3253020 and ZiZc3/XPSemu 8eec07b4,
ps5/compat/libc_stubs.c:67-73):

    void closefrom(int lowfd)
    {
        int max = getdtablesize();
        for (int fd = lowfd < 0 ? 0 : lowfd; fd < max; fd++) {
            close(fd);
        }
    }

so the value decides how many close calls run, including over the
libSceNet descriptor range. With the export the titles load; a call
ends in the throw. Predicted stops removed (VLC registry index 354
after boykopovar#3556, XPSemu 395, not run):
FAIL: unresolved ELF import sZuwaDPATKs from libkernel.prx

guest_getrlimit checks the throw.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Progress report (a11c8f7 → 028c1b4)

📈 System libraries: 85.82% (+0.01%, +3 implemented functions)

✅ 3 implemented
Library Name
libkernel getdtablesize_nid_postfix
libkernel mlockall_nid_postfix
libkernel pthread_atfork_nid_postfix
⚪ 1 behaviourally empty (constant return or throw)
Library Name
libkernel getdtablesize_nid_postfix

@blawesom

Copy link
Copy Markdown
Contributor

Independent pre-review on Linux (full CI runs only happen on manual trigger, so recording one): CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON.

  • Builds; tools/check_conventions.py --base origin/main passes.
  • Full suite: 833/833 passed (all tests including the Vulkan suites on AMD RX 9070 XT / RADV).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 11, 2026
@babyhuey babyhuey mentioned this pull request Oct 11, 2026
5 tasks done
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Closing: getdtablesize always throws, mlockall throws for every valid flag and the pthread_atfork handlers are never run, so no title gets further with this. Reopen when a title stops at one of them on main and the call it makes can be answered.

@reyani-dev reyani-dev mentioned this pull request Oct 12, 2026
10 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs libkernel needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants